package org.easyspring.util;

import org.springframework.context.annotation.Configuration;
import org.springframework.statemachine.config.EnableStateMachine;
import org.springframework.statemachine.config.EnumStateMachineConfigurerAdapter;
import org.springframework.statemachine.config.builders.StateMachineStateConfigurer;
import org.springframework.statemachine.config.builders.StateMachineTransitionConfigurer;

/**
 * @description:
 * @author：hy
 * @date: 2025/6/12
 */
@Configuration
@EnableStateMachine
public class StateMachineConfig extends EnumStateMachineConfigurerAdapter<States, Events> {
    @Override
    public void configure(StateMachineStateConfigurer<States, Events> states) throws Exception {
        states.withStates()
                .initial(States.INITIAL)
                .state(States.PROCESSING)
                .end(States.COMPLETED)
                .end(States.CANCELLED);
    }

    @Override
    public void configure(StateMachineTransitionConfigurer<States, Events> transitions) throws Exception {
        transitions
                .withExternal()
                .source(States.INITIAL).target(States.PROCESSING)
                .event(Events.START_PROCESS)
                .and()
                .withExternal()
                .source(States.PROCESSING).target(States.COMPLETED)
                .event(Events.FINISH_PROCESS)
                .and()
                .withExternal()
                .source(States.PROCESSING).target(States.CANCELLED)
                .event(Events.CANCEL_PROCESS);
    }
}
